#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::endl;
using std::istream;
using std::ifstream;
using std::string;

//基类引用绑定派生类对象
void checkStatus(const istream & is){
    cout << "is's goodbit:" << is.good() << endl;
    cout << "is's badbit:" << is.bad() << endl;
    cout << "is's failbit:" << is.fail() << endl;
    cout << "is's eofbit:" << is.eof() << endl;
    cout << endl;
}

void test0(){
    /* ifstream ifs; */
    /* ifs.open("explicit.cc"); */

    /* ifstream ifs2("explicit.cc"); */

    //文件输入流对象所绑定的文件要存在
    //如果不存在，会使文件输入流对象的状态变为failbit
    /* string filename("it.cc"); */
    string filename("explicit.cc");
    ifstream ifs(filename);
    
    checkStatus(ifs);
    
    /* string word; */
    /* ifs >> word; */
    /* cout << "word:" << word << endl; */

    string word;
    //输入流运算符以空格符、换行符作为分隔符
    //ifs >> word这个表达式的结果仍是ifs对象
    //只要ifs对象是goodbit状态，
    //作判断时就可以自动转换成true
    while(ifs >> word){
        cout << word << endl;
    }

    //关闭文件流
    ifs.close();
}

int main(void){
    test0();
    return 0;
}
